var req;

function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
            response  = req.responseXML;
            erro = response.getElementsByTagName('erro')[0].firstChild.data;
            method = response.getElementsByTagName('method')[0].firstChild.data;
            eval(method + '(response)');
            loading = document.getElementById('loading');
            loading.style.display = 'none';
        } else {
            alert("An unexpected error ocurred. Try again.");
        }
    } else {
        loading = document.getElementById('loading');
        loading.style.display = 'block';
    }
}

function addOption(response) {
    optName = response.getElementsByTagName('optName')[0].firstChild.data;
    optValue = response.getElementsByTagName('optValue')[0].firstChild.data;
    elementName = response.getElementsByTagName('elementName')[0].firstChild.data;
    
    opt = new Option(optName, optValue);
    element = document.getElementById(elementName);
    element.insertBefore(opt, element.lastChild);
    opt.selected = true;
}